//
// Copyright (C) 2008 Jackson Harper (jackson@ximian.com)

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
//

using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;

using Grr.Model;

using Gaia.WebWidgets;

namespace Grr.Web {
	public class TagCloud : System.Web.UI.WebControls.CompositeDataBoundControl
	{
		private static string[] FontSizes = new string[] { "xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large" };

		[PersistenceMode(PersistenceMode.InnerProperty)]
		public List<Tag> Items
		{
			get
			{
				object o = ViewState["_Items"];
				if (o != null)
					return o as List<Tag>;
				return new List<Tag> ();

			}
			set
			{
				ViewState["_Items"] = value;
			}   
		}

		protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
		{
			if (dataBinding)
				CreateItemsFromData (dataSource);

			int count = 0;
			double mean;
			double stddev;
			CalculateMeanAndStandardDeviation(Items.GetEnumerator (), out mean, out  stddev);

			foreach (Tag tag in Items) {
				LinkButton lb = new LinkButton();
				lb.Click += delegate(object sender, EventArgs ea)
				{
					RaiseBubbleEvent(sender, ea);
				};
				lb.Text = tag.Name;

				int norm = Normalize (tag.Instances, mean, stddev);

				lb.Style.Add(System.Web.UI.HtmlTextWriterStyle.FontSize, FontSizes [norm - 1]);

				this.Controls.Add(lb);
				this.Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));
				++count;
			}

			return count;
		}

		private void CreateItemsFromData(IEnumerable data)
		{
			List<Tag> items = new List<Tag>();
			foreach (Tag t in data)
			{
				items.Add(t);
			}
			Items = items;
		}

		private void CalculateMeanAndStandardDeviation (IEnumerator data, out double mean, out double stddev)
		{
			data.Reset();

			long sum = 0;
			long count = 0;
			while (data.MoveNext ()) {
				object o = data.Current;
				Tag t = (Tag) data.Current;
				sum += t.Instances;
				count++;
			}

			mean = sum / count;
            
			double variance = 0;

			data.Reset ();
			while (data.MoveNext ()) {
				Tag t = (Tag) data.Current;
				double diff = t.Instances - mean;
				variance += diff * diff;
			}

			stddev = Math.Sqrt(variance / count);

			data.Reset();
		}

		private static int Normalize (double weight, double mean, double stddev)
		{
			double factor = (weight - mean);

			if (factor != 0 && stddev != 0)
				factor /= stddev;

			return (factor > 2) ? 7 : (factor > 1) ? 6 : (factor > 0.5) ? 5 : (factor > -0.5) ? 4 :	(factor > -1) ? 3 : (factor > -2) ? 2 :	1;
		}


	}
}
